#pragma once
#include "CWindow/CWindowBase.h"
#include "CBitmapDraw.h"
#include <vector>

const int grid_x_count = 32;
const int grid_y_count = 30;

const DWORD DRAW_BACK_GRID_COLOR = RGB(200, 200, 200);
const DWORD DRAW_EMPTY_COLOR = RGB(255, 255, 255);
const DWORD DRAW_LINE_COLOR = RGB(255, 0, 0);
const DWORD DRAW_RECT_COLOR = RGB(255, 64, 0);
const DWORD DRAW_FILL_RECT_COLOR = RGB(0, 255, 255);
const DWORD DRAW_CIRCLE_COLOR = RGB(255, 0, 255);
const DWORD DRAW_GRID_COLOR = RGB(0, 128, 255);
const DWORD DRAW_FILL_GRID_COLOR = RGB(0, 128, 255);
const DWORD DRAW_PEN_COLOR = RGB(255, 0, 255);

#define CONFIG_REG_KEY                                      HKCU
#define CONFIG_ROOT_PATH                                    _T("Software\\CBitmapPaint")
#define CONFIG_HISTORY_DATA                                 _T("HistoryData")

enum DrawType
{
    eDTLine,
    eDTRect,
    eDTFillRect,
    eDTCircle,
    eDTGrid,
    eDTFillGrid,
    eDTPoints
};

typedef struct _DRAW_ITEM_DATA
{
    POINT ptLT;
    POINT ptRB;
    DrawType dwType;

    _DRAW_ITEM_DATA(POINT ptLT, POINT ptRB, DrawType dwType)
    {
        this->ptLT = ptLT;
        this->ptRB = ptRB;
        this->dwType = dwType;
    }

    std::vector<POINT> PointList;
}DRAW_ITEM_DATA;

class CWindowDraw: 
    public CWindowBase
{
public:

    CWindowDraw();
    CWindowDraw(const CWindowDraw& r) = delete;
    ~CWindowDraw();

    HDC GetMemDC();
    HDC GetBackMemDC();
    HBITMAP GetBitmap();
    HBITMAP GetBackBitmap();
    void SetDrawMode(DrawType dwMode = DrawType::eDTLine);
    void SetDrawGrid(bool bDrawGrid);
    void SetDrawTime(bool bDrawTime);
    void Reset();
    void UnDo();
    void ReDo();
    void SetCount(DWORD dwCount);
    void SetMode(bool bGDI = false);
    void DrawItemList();
    void DrawItemListBitmap();
    void DrawItemListGdi();

protected:
    wnd_msg LRESULT OnCreate(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnClose(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnEraseBkgnd(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnPaint(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnDestroy(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnMouseActivate(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnNcHittest(WPARAM wParam, LPARAM lParam);

    wnd_msg LRESULT OnMouseWheel(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnMbuttonDown(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnLbuttonDown(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnMouseMove(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnLbuttonUp(WPARAM wParam, LPARAM lParam);

private:

    DECLARE_WND_MESSAGE_MAP()

private:

    HDC         m_hMemDC = NULL;
    HBITMAP     m_hMemBitmap = NULL;

    HDC         m_hBackMemDC = NULL;
    HBITMAP     m_hBackMemBitmap = NULL;
    HFONT       m_hFont = NULL;

    POINT       m_ptDown = {0};
    DrawType    m_dwType = DrawType::eDTLine;
    DWORD       m_dwCount = 1;

    bool        m_bGDIMode = false;
    bool        m_bDrag = false;
    bool        m_bDrawGrid = false;
    bool        m_bDrawTime = false;

    std::vector<DRAW_ITEM_DATA> m_drawItemList;
    std::vector<POINT> m_PointList;
    int         m_nDrawIndex = 0;
};